home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / SmallTalk / Character.st < prev    next >
Text File  |  1995-08-25  |  6KB  |  237 lines

  1. "======================================================================
  2. |
  3. |   Character Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbb         22 Sep 90      Changed character to always print with a leading $.
  36. |
  37. | sbyrne     16 May 90      Changed from being variableSubclass to
  38. |              variableWordSubclass.
  39. |
  40. | sbyrne     25 Apr 89      created.
  41. |
  42. "
  43.  
  44.  
  45. "*** Having this be variable seems wrong ***"
  46. Magnitude variableWordSubclass: #Character 
  47.       instanceVariableNames: ''
  48.       classVariableNames: ''
  49.       poolDictionaries: ''
  50.       category: nil
  51. !
  52.  
  53. Character comment: 
  54. 'My instances represent the 256 characters of the character set.  I provide
  55. messages to translate between integers and character objects, and provide
  56. names for some of the common unprintable characters.' !
  57.  
  58.  
  59. !Character class methodsFor: 'constants'!
  60.  
  61. backspace
  62.     "Returns the character 'backspace'"
  63.     ^Character value: 8
  64. !
  65.  
  66. tab
  67.     "Returns the character 'tab'"
  68.     ^Character value: 9
  69. !
  70.  
  71. nl
  72.     "Returns the character 'nl'"
  73.     ^Character value: 10
  74. !
  75.  
  76. newPage
  77.     "Returns the character 'newPage'"
  78.     ^Character value: 12
  79. !
  80.  
  81. cr
  82.     "Returns the character 'cr'"
  83.     ^Character value: 13
  84. !
  85.  
  86. esc
  87.     "Returns the character 'esc'"
  88.     ^Character value: 27
  89. !
  90.  
  91. space
  92.     "Returns the character 'space'"
  93.     ^Character value: 32    "could also return $ "
  94. !!
  95.  
  96.  
  97. !Character class methodsFor: 'Instance creation'!
  98.  
  99. digitValue: anInteger
  100.     "Returns a character that corresponds to anInteger.  0-9 map to $0-$9,
  101.     10-35 map to $A-$Z"
  102.     (anInteger between: 0 and: 9)
  103.         ifTrue: [ ^Character value: anInteger + 48 ]. "48 == $0"
  104.     (anInteger between: 10 and: 35)
  105.         ifTrue: [ ^Character value: anInteger - 10 + 65 ]. "65 = $A"
  106.     ^self error: 'value not in range 0 to 35'
  107. !!
  108.  
  109.  
  110.  
  111. !Character methodsFor: 'converting'!
  112.  
  113. digitValue
  114.     "Returns the value of self interpreted as a digit.  Here, 'digit' means
  115.     either 0-9, or uppercase A-Z, which maps to 10-35."
  116.     (self between: $0 and: $9) ifTrue: [ ^self asciiValue - 48 ].
  117.                 "48 is $0"
  118.     (self between: $A and: $Z) ifTrue: [ ^self asciiValue - 65 + 10 ].
  119.                 "65 is $A"
  120.     ^self error: 'Invalid digit character'
  121. !!
  122.  
  123.  
  124.  
  125. !Character methodsFor: 'comparing'!
  126.  
  127. < aCharacter
  128.     ^self asciiValue < aCharacter asciiValue
  129. !
  130.  
  131. <= aCharacter
  132.     ^self asciiValue <= aCharacter asciiValue
  133. !
  134.  
  135. > aCharacter
  136.     ^self asciiValue > aCharacter asciiValue
  137. !
  138.  
  139. >= aCharacter
  140.     ^self asciiValue >= aCharacter asciiValue
  141. !!
  142.  
  143.  
  144.  
  145. !Character methodsFor: 'testing'!
  146.  
  147. isDigit
  148.     "True if self is a 0-9 digit"
  149.     ^self between: $0 and: $9
  150. !
  151.  
  152. isLetter
  153.     "True if self is an upper- or lowercase letter"
  154.     ^(self between: $a and: $z) or: [ self between: $A and: $Z ]
  155. !
  156.  
  157. isAlphaNumeric
  158.     "True if self is a letter or a digit"
  159.     ^self isLetter or: [ self isDigit ]
  160. !
  161.  
  162. isLowercase
  163.     "True if self is a lowercase letter"
  164.     ^self between: $a and: $z
  165. !
  166.  
  167. isUppercase
  168.     "True if self is uppercase"
  169.     ^self between: $A and: $Z
  170. !
  171.  
  172. isSeparator
  173.     "Returns true if self is a space, cr, tab, nl, or newPage"
  174.     self > Character space ifTrue: [ ^false ].
  175.     self = Character space ifTrue: [ ^true ].
  176.     self = Character cr ifTrue: [ ^true ].
  177.     self = Character tab ifTrue: [ ^true ].
  178.     self = Character nl ifTrue: [ ^true ].
  179.     self = Character newPage ifTrue: [ ^true ].
  180.     ^false
  181. !
  182.  
  183. isVowel
  184.     "Returns true if self is a, e, i, o, or u; case insensitive"
  185.     | char |
  186.     char _ self asLowercase.
  187.     char = $a ifTrue: [ ^true ].    
  188.     char = $e ifTrue: [ ^true ].    
  189.     char = $i ifTrue: [ ^true ].    
  190.     char = $o ifTrue: [ ^true ].    
  191.     char = $u ifTrue: [ ^true ].    
  192.     ^false
  193. !!
  194.  
  195.  
  196.  
  197. !Character methodsFor: 'Coercion methods'!
  198.  
  199. asLowercase
  200.     "Returns self as a lowercase character if it's an uppercase letter,
  201.      otherwise returns the character unchanged."
  202.     self isUppercase ifFalse: [ ^self ].
  203.     ^Character value: (self asciiValue) + 32
  204. !
  205.  
  206. asUppercase
  207.     "Returns self as a uppercase character if it's an lowercase letter,
  208.      otherwise returns the character unchanged."
  209.     self isLowercase ifFalse: [ ^self ].
  210.     ^Character value: (self asciiValue) - 32
  211. !
  212.  
  213. asSymbol
  214.     "Returns the character self as a symbol."
  215.     ^Symbol internCharacter: self
  216. !!
  217.  
  218.  
  219.  
  220. !Character methodsFor: 'printing'!
  221.  
  222. printOn: aStream
  223.     self storeOn: aStream
  224. !!
  225.  
  226.  
  227.  
  228. !Character methodsFor: 'storing'!
  229.  
  230. storeOn: aStream
  231.     aStream nextPut: $$.
  232.     aStream nextPut: self
  233. !!
  234.  
  235.